﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Media.TextFormatting;
using WpfDemo.Common;
using WpfDemo.IRepository.Sys;
using WpfDemo.Models;
using WpfDemo.Models.Sys;
using WpfDemo.Repository.Sys;
using WpfDemo.View.Sys.User;

namespace WpfDemo.ViewModel.Sys
{
    public partial class AuthViewModel: BaseViewModel
    {
        IModuleRepository _repository;

        IAuthRepository _authrepository;
        public AuthViewModel(IModuleRepository repository, IAuthRepository authRepository)
        {
            this._repository = repository;     
            this._authrepository = authRepository;
            GetPageList();
        }

      

        /// <summary>
        /// 菜单
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<MenuItemModel> menus;

        [ObservableProperty]
        private bool _isEnabled = true;

        [ObservableProperty]
        private MenuItemModel moduleEntity;

      
        [ObservableProperty]
        private string keyStr = string.Empty;


        [ObservableProperty]
        private string roleId = string.Empty;

        /// <summary>
        /// 获取当前路径
        /// </summary>
        public string CurrentUserPermission
        {
            get
            {

                return System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName;
            }
            set { CurrentUserPermission = value; }
        }


        /// <summary>
        /// 反选
        /// </summary>

        [RelayCommand]
        private async void AllSelect()
        {
            foreach (var item in Menus)
            {
                if (item.Children != null && item.Children.Count > 0)
                {
                    foreach (var item2 in item.Children)
                    {
                        foreach (var item1 in item2.Buttons)
                        {
                            item1.IsChecked = !item1.IsChecked;
                        }
                    }

                }
            }
        }

        /// <summary>
        /// 单选全选
        /// </summary>

        [RelayCommand]
        private async void RowSelect(object F_Id)
        {
            foreach (var item in Menus)
            {               
                if(item.F_Id == F_Id.ToString())
                {
                    if(item.Children!=null&& item.Children.Count>0)
                    {
                        foreach (var item2 in item.Children)
                        {
                            foreach (var item1 in item2.Buttons)
                            {
                                item1.IsChecked = !item1.IsChecked;
                            }
                        }                       
                    }

                }
                else
                {
                    if (item.Children != null && item.Children.Count > 0)
                    {
                        foreach (var item2 in item.Children)
                        {
                            if(item2.F_Id == F_Id.ToString())
                            {
                                foreach (var item1 in item2.Buttons)
                                {
                                    item1.IsChecked = !item1.IsChecked;
                                }
                            }
                           
                        }

                    }
                }
               
            }
        }


        [RelayCommand]
        public void GetPageList()
        {   

            Expression<Func<ModuleEntity, bool>> Wherelambda = p => true;
            if (!string.IsNullOrEmpty(keyStr))
            {
                Wherelambda = Wherelambda.And(x =>  x.F_Name.Contains(keyStr));
            }
            Expression<Func<ModuleEntity, string>> OrderBylambda = c => c.F_Id;
            var list = _repository.GetMenus().OrderBy(m=>m.F_Sort).ToList();
            var buttons= _authrepository.Query().Where(m=>m.F_RoleId==roleId).ToList();
            var pageLst = new List<ModuleEntity>();
            list.ForEach(item =>
            {
                var items = item.menus;
                if(items!=null && items.Count > 0)
                {
                    foreach (var item1 in items)
                    {
                        var cnt = buttons.Where(m => m.F_ButtonId == item1.ButtonId).Count();
                        if(cnt > 0)
                        {
                            item1.IsChecked = true;
                        }
                    }
                }

                pageLst.Add(new ModuleEntity()
                {
                    F_Code = item.F_Code,
                    F_CreateTime = item.F_CreateTime,
                    F_Icon = item.F_Icon,
                    F_Id = item.F_Id,
                    F_IsDisabled = item.F_IsDisabled,
                    F_IsExpanded = item.F_IsExpanded,
                    F_Name = item.F_Name,
                    F_ParentId = item.F_ParentId,
                    
                    menus = items,
                });
            });

            Menus = new ObservableCollection<MenuItemModel>();
            MenuHelper.FillMenus(Menus, null, pageLst);
            //AllSelect();
        }

        [ObservableProperty]
        private Object selectedItem;

       

        [RelayCommand]
        private async void Save(object obj)
        {
            var lst = new List<AuthEntity>();
            foreach (var item in Menus)
            {
                if (item.Children != null && item.Children.Count > 0)
                {
                    foreach (var item2 in item.Children)
                    {
                        foreach (var item1 in item2.Buttons)
                        {                           
                            if(item1.IsChecked.Value)
                            {
                                lst.Add(new AuthEntity()
                                {
                                    F_Id= Guid.NewGuid().ToString("N").ToUpper(),
                                    F_RoleId = RoleId,
                                    F_ButtonId = item1.ButtonId,
                                    F_ModuleId= item2.F_Id,
                                });
                            }
                        }
                    }

                }
            }
            _authrepository.Add(lst, RoleId);

            WindowManager.CloseFrm("AuthIndex", null);
            MessageHelper.ShowSuccess("保存成功！");
        }

       
    }
}
